﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace ECSE6770.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            return View(Db.Products.Where(p => p.Active && p.Featured && !p.Deleted));
        }

#if DEBUG
        public ActionResult VideoTest()
        {
            return View();
        }
#endif

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Modules()
        {
            return View(Db.Modules.ToList().Where(m => !m.IsPast).OrderBy(m => m.Start).ToList());
        }

        public ActionResult ModuleDetails(int id)
        {
            return View(Db.Modules.First(m => m.Id == id));
        }

        public ActionResult Videos()
        {
            return View(Db.Videos.OrderBy(v => v.Title).ToList());
        }

        public ActionResult ViewVideo(int id)
        {
            return View(Db.Videos.First(v => v.Id == id));
        }

#if DEBUG

        public ActionResult AddModule(int moduleId, string userName)
        {
            var user = ContextManager.CurrentDb.UserInterops.First(u => u.UserName.Equals(userName, StringComparison.InvariantCultureIgnoreCase));
            var prod = ContextManager.CurrentDb.Modules.First(m => m.Id == moduleId);
            user.PurchasedProducts.Add(prod);
            ContextManager.CurrentDb.SaveChanges();
            return new EmptyResult();
        }

        public ActionResult SetupDatabase()
        {
            if (!Roles.RoleExists("Admin"))
                Roles.CreateRole("Admin");
            if (!Roles.RoleExists("Trainer"))
                Roles.CreateRole("Trainer");

            //create a super admin account, if needed
            var sa = Membership.GetUser("sa");
            if (sa == null)
            {
                sa = Membership.CreateUser("sa", "hello", "sanviw@rpi.edu");
                Roles.AddUserToRole("sa", "Admin");
            }

            EntitiesContainer container = new EntitiesContainer();
            if (container.Levels.Count() == 0)
            {
                container.Levels.Add(new Level { Description = "Level A", HelpText = "Level A is for those with solid experience in the subject matter." });
                container.Levels.Add(new Level { Description = "Level B", HelpText = "Level B is directed towards those with a light familiarity of the subject." });
                container.Levels.Add(new Level { Description = "Level C", HelpText = "Level C is an introductory level intended for all participants." });
                container.SaveChanges();
            }

            //sync users
            var interopSet = container.UserInterops.ToLookup(u => u.UserName);
            foreach (MembershipUser u in Membership.GetAllUsers())
            {
                if (!interopSet.Contains(u.UserName))
                    container.UserInterops.Add(new UserInterop { UserName = u.UserName });
            }
            container.SaveChanges();

            //add some test venues
            if (container.Venues.Count() == 0)
            {
                container.Venues.Add(new Venue { Name = "RPI Hartford", Address = "275 Windsor St.", Country = "United States", City = "Hartford", State = "CT", ZipCode = "06120", LocalAccommodations = "<p>This is a sample.</p>" });
                container.Venues.Add(new Venue { Name = "Sample Venue", Address = "1 Main St.", Country = "United States", City = "Hartford", State = "CT", ZipCode = "06120", LocalAccommodations = "<p>This is a sample.</p>" });
                container.SaveChanges();
            }

            return new EmptyResult();
        }
#endif
    }
}
